package cn.scw.project.system.service.impl;

import cn.scw.common.constant.SysConsts;
import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.SecurityUtils;
import cn.scw.common.utils.StringUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.project.system.domain.SysUser;
import cn.scw.project.system.domain.SysUserExtract;
import cn.scw.project.system.domain.netework.bean.SysUserExtractAddReq;
import cn.scw.project.system.domain.netework.bean.SysUserExtractUpdReq;
import cn.scw.project.system.domain.vo.SysUserExtractVo;
import cn.scw.project.system.mapper.SysUserExtractMapper;
import cn.scw.project.system.service.ISysUserExtractService;
import cn.scw.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户提现记录Service业务层处理
 *
 * @author api
 * @date 2020-05-20
 */
@Service
public class SysUserExtractServiceImpl implements ISysUserExtractService {
    @Autowired
    private SysUserExtractMapper sysUserExtractMapper;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询用户提现记录
     *
     * @param id 用户提现记录ID
     * @return 用户提现记录
     */
    @Override
    public SysUserExtract selectSysUserExtractById(Long id) {
        return sysUserExtractMapper.selectSysUserExtractById(id);
    }

    @Override
    public SysUserExtractVo selectSysUserExtractVoById(Long id) {
        return sysUserExtractMapper.selectSysUserExtractVoById(id);
    }

    /**
     * 查询用户提现记录列表
     *
     * @param status 用户提现记录
     * @return 用户提现记录
     */
    @Override
    public List<SysUserExtractVo> selectSysUserExtractList(String  status) {
        return sysUserExtractMapper.selectSysUserExtractList(status);
    }

    @Override
    public List<SysUserExtractVo> selectSysUserExtractListByUser() {
        return sysUserExtractMapper.selectSysUserExtractListByUser(SecurityUtils.getLoginUser().getUser().getUserId());
    }

    /**
     * 新增用户提现记录
     *
     * @param req 用户提现记录
     * @return 结果
     */
    @Override
    public int insertSysUserExtract(SysUserExtractAddReq req) {
      SysUser user = iSysUserService.getCurrentUser();
        double money = user.getMoney();
        if (money < req.getMoney())
            throw new CustomException("用户余额不足");
        if (StringUtils.isBlank(req.getQrCode()))
            throw new CustomException("请上传收款二维码");
        SysUserExtract extract = new SysUserExtract();
        user.setMoney(money - req.getMoney());
        ToolUtils.copyPropertiesIgnoreNull(req, extract);
        extract.setUserId(user.getUserId());
        extract.setStatus(SysConsts.EXTRACT_STATUS_PENDING);
        iSysUserService.updateUserInfo(user);

        return sysUserExtractMapper.insertSysUserExtract(extract);
    }

    /**
     * 修改用户提现记录
     *
     * @param req 用户提现记录
     * @return 结果
     */
    @Override
    public int updateSysUserExtract(SysUserExtractUpdReq req) {
        SysUserExtract extract = this.selectSysUserExtractById(req.getId());
        if (extract == null)
            throw new CustomException("提现记录不存在");
        if (req.isAgree()) {
            extract.setStatus(SysConsts.EXTRACT_STATUS_SUCCESS);
            extract.setDisposeDate(new Date());
        } else {
            if (StringUtils.isBlank(req.getNote()))
                throw new CustomException("请填写原因");
            extract.setStatus(SysConsts.EXTRACT_STATUS_NO_SUCCESS);
            extract.setDisposeDate(new Date());
            SysUser user = SecurityUtils.getLoginUser().getUser();
            user.setMoney(user.getMoney() + extract.getMoney());
            iSysUserService.updateUserInfo(user);
        }
        extract.setNote(req.getNote());

        return sysUserExtractMapper.updateSysUserExtract(extract);
    }

    /**
     * 批量删除用户提现记录
     *
     * @param ids 需要删除的用户提现记录ID
     * @return 结果
     */
    @Override
    public int deleteSysUserExtractByIds(Long[] ids) {
        return sysUserExtractMapper.deleteSysUserExtractByIds(ids);
    }

    /**
     * 删除用户提现记录信息
     *
     * @param id 用户提现记录ID
     * @return 结果
     */
    @Override
    public int deleteSysUserExtractById(Long id) {
        return sysUserExtractMapper.deleteSysUserExtractById(id);
    }
}
